package ca.uwindsor.cs.deepweb.utility;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.TermFreqVector;

import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;

public class NullContentDocumentRemover {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 1) {
			System.exit(-1);
		}
		try {
			int numDocs, i;
			IndexReader indexreader = IndexReader.open(args[0]);
			numDocs = indexreader.numDocs();
			TermFreqVector termFreqVector;
			for (i = 0; i < numDocs; i++) {
				termFreqVector = indexreader.getTermFreqVector(i,
						FilesContentIndexer.FIELD_CONTENT);
				if (termFreqVector == null) {
					indexreader.deleteDocument(i);
				}
			}
			indexreader.close();

			IndexWriter indexWriter;
			Analyzer luceneAnalyzer = new StandardAnalyzer();
			indexWriter = new IndexWriter(args[0], luceneAnalyzer, false);
			indexWriter.optimize();
			System.out.println("optimization is completed!");
			indexWriter.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);
		}

	}

}
